package presentation.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import persistence.Purchaser;
import business.BusinessObjectAssembler;
import business.SubscriptionLogic;

public class PurchaserConverter implements Converter {

	private static SubscriptionLogic subscriptionLogic; 
	
	static {
		subscriptionLogic = BusinessObjectAssembler.getSubscriptionLogic();
	}
	
	@Override
	public Object getAsObject(FacesContext fctx, UIComponent comp, String value)
			throws ConverterException {
		Purchaser purchaser = null;
		if (value != null && ConverterUtil.isNumber(value)) {
			try {
				purchaser = subscriptionLogic.getPurchaserById(Integer.valueOf(value).intValue());
				System.out.println(purchaser);
			}
			catch (Exception e) {
				throw new ConverterException(e);
			}
		}
		return purchaser;
	}

	@Override
	public String getAsString(FacesContext fctx, UIComponent comp, Object value)
			throws ConverterException {
		String retVal = "";
		Purchaser purchaser = (Purchaser) value;
		if (purchaser != null)
			retVal = String.valueOf(purchaser.getId());
		return retVal;
	}

}